﻿using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using 畜牧业审核程序国产.Models;


using Nlnet.Avalonia.Controls;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using 畜牧业审核程序国产.Views;
using Avalonia.Controls;
using 畜牧业审核程序国产.Models.ToolsClass;
using System.Drawing;
using Brushes = Avalonia.Media.Brushes;
using Icon = MsBox.Avalonia.Enums.Icon;
using System;
using System.Data;
using System.Threading.Tasks;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia;
using 畜牧业审核程序国产.Views.PreReviewView;


namespace 畜牧业审核程序国产.ViewModels;

public partial class MainWindowViewModel : ObservableObject
{
#pragma warning disable CA1822 // Mark members as static
    public string Greeting => "Welcome to Avalonia!";
#pragma warning restore CA1822 // Mark members as static

    #region 打开页面的方法

    public void OpenPage(string PageName)
    {
        if (PageName.Equals("1"))
        {
            CurrentPage = PG1;
            //Debug.Print(PageName+Page.ToString());
            //ActiveButton = "1";
            IBrush[] UnfocousColors = { Color2, Color3, Color4, Color5, Color6, Color7 };
            //ChangeBtnColor(UnfocousColors, Color1);

            //for (int i = 0; i < UnfocousColors.Length; i++)
            //{
            //    UnfocousColors[i] = Brushes.LightGray;
            //}
            Color1 = ActiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
            //MyMessagebox(PageName + "打开");
        }
        else if (PageName.Equals("2"))
        {
            if (PG2 == null) PG2 = new Page2();
            CurrentPage = PG2;
            //ActiveButton = "2";
            Color1 = InactiveButtonBackgroundColor;
            Color2 = ActiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
            //  MyMessagebox(PageName + "打开");
        }
        else if (PageName.Equals("3"))
        {
            if (PG3 == null) PG3 = new Page3();
            CurrentPage = PG3;
            Color1 = InactiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = ActiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
            // MyMessagebox(PageName + "打开");
        }
        else if (PageName.Equals("4"))
        {
            if (PG4 == null) PG4 = new Page4();
            CurrentPage = PG4;
            Color1 = InactiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = ActiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
        }
        else if (PageName.Equals("5"))
        {
            if (PG5 == null) PG5 = new Page5();
            CurrentPage = PG5;
            Color1 = InactiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = ActiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
        }
        else if (PageName.Equals("6"))
        {
            if (PG6 == null) PG6 = new Page6();
            CurrentPage = PG6;
            Color1 = InactiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = ActiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
        }
        else if (PageName.Equals("7"))
        {
            if (PG7 == null) PG7 = new Page7();
            CurrentPage = PG7;
            Color1 = InactiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = ActiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
        }
        else if (PageName.Equals("8"))
        {
            if (PG8 == null) PG8 = new Page8Introduction();
            CurrentPage = PG8;
            Color1 = InactiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = ActiveButtonBackgroundColor;
            Color9 = InactiveButtonBackgroundColor;
        }
        else if (PageName.Equals("9"))
        {
            if(PG9 == null) PG9 = new Page9MarketInfo();
            CurrentPage = PG9;
            Color1 = InactiveButtonBackgroundColor;
            Color2 = InactiveButtonBackgroundColor;
            Color3 = InactiveButtonBackgroundColor;
            Color4 = InactiveButtonBackgroundColor;
            Color5 = InactiveButtonBackgroundColor;
            Color6 = InactiveButtonBackgroundColor;
            Color7 = InactiveButtonBackgroundColor;
            Color8 = InactiveButtonBackgroundColor;
            Color9 = ActiveButtonBackgroundColor;
        }
    }

    #endregion


    #region 声明命令

    public RelayCommand<string> OpenCommand { get; private set; } //打开右侧页面的命令
    public RelayCommand NavigateCommand { get; }
    public RelayCommand<string> ToggleFlagCommand { get; }
    public RelayCommand OpenRagDialogCommand { get; private set; }
    public RelayCommand OpenTestPythonCommand { get; private set; }
    public RelayCommand OpenPreReviewCommand { get; private set; }

    #endregion

    #region 声明需要在右侧展示的视图名

    public static Page1 PG1; //=new View.Page1();//默认打开视图1,只会在第一次访问时初始化
    public static Page2 PG2;
    public static Page3 PG3;
    public static Page4 PG4;
    public static Page5 PG5;
    public static Page6 PG6;
    public static Page7 PG7;
    public static Page8Introduction PG8;
    public static Page9MarketInfo PG9;
    #endregion

    #region 显示按钮背景色的参数

    private IBrush _color1;

    public IBrush Color1
    {
        get => _color1;
        set => SetProperty(ref _color1, value);
    }

    private IBrush _color2;

    public IBrush Color2
    {
        get => _color2;
        set => SetProperty(ref _color2, value);
    }

    [ObservableProperty] private IBrush _color3;

    [ObservableProperty] private IBrush _color4;

    [ObservableProperty] private IBrush _color5;

    [ObservableProperty] private IBrush _color6;

    [ObservableProperty] private IBrush _color7;

    [ObservableProperty] private IBrush _color8;
    [ObservableProperty] private IBrush _color9;

    #endregion


    #region 当前显示的页面属性

    private object _currentPage;

    public object CurrentPage
    {
        get => _currentPage;
        set => SetProperty(ref _currentPage, value);
    }
    public IBrush ActiveButtonBackgroundColor {
        get => Brushes.GhostWhite;
           }
    public IBrush InactiveButtonBackgroundColor { get => Brushes.LightGray; }
    #endregion

    #region 构造函数
    // private Window _window;
    public MainWindowViewModel()//Window window)
    {
        CurrentPage = PG1 = new Page1();
        InitializeColor();
        OpenCommand = new RelayCommand<string>(p => OpenPage(p));
       // this.OpenRagDialogCommand = new RelayCommand(async ()=> await OpenRagDialogAsync());
       this.OpenRagDialogCommand = new RelayCommand(() => OpenRagDialogAsync());
        this.OpenTestPythonCommand = new RelayCommand(async () => await OpenTestPython());
        this.OpenPreReviewCommand = new RelayCommand(async () => await OpenPreReviewView());
        //_window = window;
        //将修改的内容写到数据库中对应的位置,该方法无需执行了
        //ModifySomeDataInDataBase();
        WeakReferenceMessenger.Default.Register<MyMessage>(this, OnMyMessageReceived);
    }

    private async Task OpenTestPython()
    {
        Window testPythonWindow = new TestPythonView();
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            await testPythonWindow.ShowDialog(desktop.MainWindow);
        }
    }
    private async Task OpenPreReviewView()
    {
        Window pereReviewWindow = new  PreReview();
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {

            await pereReviewWindow.ShowDialog(desktop.MainWindow);
        }
    }

    public void OpenRagDialogAsync()
    {
        // Window choseRagWindow = new ChoseRagWindow();
       string _apiKey = "sk-4487d1c2764d4c388af7a241ed28c7f4";
       string _apiUrl = "https://dashscope.aliyuncs.com/api/v1/apps/c08d698e5e9e48d7bf36cc184ac8df10/completion";
       string _ragModel = "通义千问业务知识库";
        ChatWindow chatWindow =new ChatWindow(_apiKey,_apiUrl,_ragModel );
        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            //await choseRagWindow.ShowDialog(desktop.MainWindow);
             chatWindow.Show();
        }
      

   
  
    }
    private void ModifySomeDataInDataBase()
    {
        try
        {
            MyDBOperator modifyOperator = MyDBOperator.DBOperatorInstance;
            //  string sqlToGetNeedModifyFormula = "select 审核公式说明 from 审核公式A212 where 审核公式名称 = '家禽单价过高或高低'";
            //  DataTable dtOfA212Formula = modifyOperator.GetDataTable(sqlToGetNeedModifyFormula);
            //  string? 审核公式说明 = dtOfA212Formula.Rows[0][0].ToString();
            //  //修改家禽单价
            //  if (审核公式说明.Contains("9元"))

            //  {
            //      string sqlToModifyFormula = "UPDATE 审核公式A212 SET 审核公式说明 = '家禽单价过高或高低。单价低于单价7元/公斤或高于单价45元/公斤，需要核实。',sql语句 = '" +
            //          "select 县,乡,村,户主姓名,出售家禽金额（元）11 as 本季度出售家禽金额（元）11,出售家禽重量（公斤）13  as 本季度出售家禽重量（公斤）13 ,round([出售家禽金额（元）11]/[出售家禽重量（公斤）13],2) as [家禽单价（元/公斤）] from (select * from 查询用表 where [出售家禽重量（公斤）13]>0 and [出售家禽重量（公斤）13] is not null) where [出售家禽金额（元）11]/[出售家禽重量（公斤）13] not between 7 and 45',关键指标值1 = '7'" +
            //          " where 审核公式名称 = '家禽单价过高或高低'";
            //      modifyOperator.ExecuteBySql(sqlToModifyFormula);
            //  }
            //  //鸭和鹅出栏
            //  string sqlToGetNeedModifyFormula鸭鹅出栏 = "select 审核公式说明 from 审核公式A212 where 审核公式名称 LIKE '%鸭和鹅%' and 审核公式名称 LIKE '%出栏%'";
            //  dtOfA212Formula = modifyOperator.GetDataTable(sqlToGetNeedModifyFormula鸭鹅出栏);
            //  if (dtOfA212Formula.Rows.Count > 1)
            //  {
            //      string sqlToDelete = "delete from 审核公式A212 where 审核公式名称 LIKE '%鸭和鹅出栏%'";
            //      modifyOperator.ExecuteBySql(sqlToDelete);
            //      string sqlToAdd鸭鹅出栏 = "INSERT INTO 审核公式A212 (表间关系,是否启用,严重度,审核公式名称,审核公式说明,sql语句) Values ('规模户与全面','是','强制性','规模户的鸭和鹅出栏只数大于A406全面报表的鸭和鹅出栏数','212表鸭和鹅出栏数(自宰家禽+出售家禽-自宰活鸡-出售活鸡)应大于等于406表的鸭和鹅出栏数(家禽出栏-鸡出栏)'," +
            //         "  'select t1.县,t1.乡, t1.家禽出栏只数10 as A406家禽出栏只数,t1.鸡出栏只数24  as A406鸡出栏只数,(t1.家禽出栏只数10-t1.鸡出栏只数24) as 鸭鹅出栏只数,t2.规模户家禽出栏之和,t2.规模户鸡出栏之和,t2.规模户鸭鹅出栏之和  from 全面用表 t1,(select 乡,乡镇代码,sum(自宰家禽数量07)+sum(出售家禽数量09) as 规模户家禽出栏之和,sum(自宰活鸡数量08)+sum(出售活鸡数量10) as 规模户鸡出栏之和, sum(自宰家禽数量07)+sum(出售家禽数量09)-sum(自宰活鸡数量08)-sum(出售活鸡数量10) as 规模户鸭鹅出栏之和 from  查询用表 group by 乡,乡镇代码) t2 where t1.乡镇代码  = t2.乡镇代码 and (t1.家禽出栏只数10-t1.鸡出栏只数24)-t2.规模户鸭鹅出栏之和<0'  )";
            //      modifyOperator.ExecuteBySql(sqlToAdd鸭鹅出栏);

            //  }
            //  else if (dtOfA212Formula == null || dtOfA212Formula.Rows.Count == 0)
            //  {
            //      string sqlToAdd鸭鹅出栏 = "INSERT INTO 审核公式A212 (表间关系,是否启用,严重度,审核公式名称,审核公式说明,sql语句) Values ('规模户与全面','是','强制性','规模户的鸭和鹅出栏只数大于A406全面报表的鸭和鹅出栏数','212表鸭和鹅出栏数(自宰家禽+出售家禽-自宰活鸡-出售活鸡)应大于等于406表的鸭和鹅出栏数(家禽出栏-鸡出栏)'," +
            //             "  'select t1.县,t1.乡, t1.家禽出栏只数10 as A406家禽出栏只数,t1.鸡出栏只数24  as A406鸡出栏只数,(t1.家禽出栏只数10-t1.鸡出栏只数24) as 鸭鹅出栏只数,t2.规模户家禽出栏之和,t2.规模户鸡出栏之和,t2.规模户鸭鹅出栏之和  from 全面用表 t1,(select 乡,乡镇代码,sum(自宰家禽数量07)+sum(出售家禽数量09) as 规模户家禽出栏之和,sum(自宰活鸡数量08)+sum(出售活鸡数量10) as 规模户鸡出栏之和, sum(自宰家禽数量07)+sum(出售家禽数量09)-sum(自宰活鸡数量08)-sum(出售活鸡数量10) as 规模户鸭鹅出栏之和 from  查询用表 group by 乡,乡镇代码) t2 where t1.乡镇代码  = t2.乡镇代码 and (t1.家禽出栏只数10-t1.鸡出栏只数24)-t2.规模户鸭鹅出栏之和<0'  )";
            //      modifyOperator.ExecuteBySql(sqlToAdd鸭鹅出栏);
            //  }
            //  //鸭和鹅存栏

            //string  sqlToGetNeedModifyFormula鸭和鹅存栏 = "select 审核公式说明 from 审核公式A212 where 审核公式名称 LIKE '%鸭和鹅%' and 审核公式名称 LIKE '%存栏%'";
            //  dtOfA212Formula = modifyOperator.GetDataTable(sqlToGetNeedModifyFormula鸭和鹅存栏);
            //  if (dtOfA212Formula.Rows.Count > 1)
            //  {
            //      string sqlToDelete = "delete from 审核公式A212 where 审核公式名称 LIKE '%鸭和鹅%' and 审核公式名称 LIKE '%存栏%'";
            //      modifyOperator.ExecuteBySql(sqlToDelete);
            //      string sqlToAdd鸭鹅存栏 = "INSERT INTO 审核公式A212 (表间关系,是否启用,严重度,审核公式名称,审核公式说明,sql语句) Values ('规模户与全面','是','强制性','规模户的鸭和鹅存栏只数大于A406全面报表的鸭和鹅存栏数','212表鸭和鹅存栏数(家禽存栏减去鸡存栏数)应大于等于406表的鸭和鹅存栏数(家禽存栏数减去鸡存栏数)'," +
            //        "  'select t1.县,t1.乡, t1.家禽存栏只数09 as A406家禽存栏只数,t1.鸡存栏只数23 as A406鸡存栏只数,  (t1.家禽存栏只数09 -t1.鸡存栏只数23) as [406表鸭鹅只数],t2.规模户家禽存栏之和,t2.规模户鸡存栏之和,t2.规模户鸭鹅存栏只数  from 全面用表 t1,(select 乡,乡镇代码,sum(期末存栏只数01) as 规模户家禽存栏之和,sum(肉鸡存栏只数03)+sum(蛋鸡存栏只数05) as 规模户鸡存栏之和,(sum(期末存栏只数01) -sum(肉鸡存栏只数03)-sum(蛋鸡存栏只数05))  AS 规模户鸭鹅存栏只数 from  查询用表 group by 乡,乡镇代码) t2 where t1.乡镇代码  = t2.乡镇代码 and t2.规模户鸭鹅存栏只数 >  (t1.家禽存栏只数09 -t1.鸡存栏只数23)'  )";
            //      modifyOperator.ExecuteBySql(sqlToAdd鸭鹅存栏);

            //  }
            //  else if (dtOfA212Formula == null || dtOfA212Formula.Rows.Count == 0)
            //  {
            //      string sqlToAdd鸭鹅存栏 = "INSERT INTO 审核公式A212 (表间关系,是否启用,严重度,审核公式名称,审核公式说明,sql语句) Values ('规模户与全面','是','强制性','规模户的鸭和鹅存栏只数大于A406全面报表的鸭和鹅存栏数','212表鸭和鹅存栏数(家禽存栏减去鸡存栏数)应大于等于406表的鸭和鹅存栏数(家禽存栏数减去鸡存栏数)'," +
            //             "  'select t1.县,t1.乡, t1.家禽存栏只数09 as A406家禽存栏只数,t1.鸡存栏只数23 as A406鸡存栏只数,  (t1.家禽存栏只数09 -t1.鸡存栏只数23) as [406表鸭鹅只数],t2.规模户家禽存栏之和,t2.规模户鸡存栏之和,t2.规模户鸭鹅存栏只数  from 全面用表 t1,(select 乡,乡镇代码,sum(期末存栏只数01) as 规模户家禽存栏之和,sum(肉鸡存栏只数03)+sum(蛋鸡存栏只数05) as 规模户鸡存栏之和,(sum(期末存栏只数01) -sum(肉鸡存栏只数03)-sum(蛋鸡存栏只数05))  AS 规模户鸭鹅存栏只数 from  查询用表 group by 乡,乡镇代码) t2 where t1.乡镇代码  = t2.乡镇代码 and t2.规模户鸭鹅存栏只数 >  (t1.家禽存栏只数09 -t1.鸡存栏只数23)'  )";
            //      modifyOperator.ExecuteBySql(sqlToAdd鸭鹅存栏);
            //  }
            string modifyformula = "UPDATE 审核公式A212 SET  sql语句 = 'SELECT  县,  乡,  村, 户主姓名, 当季家禽出栏数,期末存栏只数01, CASE   WHEN 期末存栏只数01 = 0 THEN 当季家禽出栏数 " +
         " ELSE ROUND(当季家禽出栏数 / NULLIF(期末存栏只数01, 0), 2) END AS 出栏比例 FROM  查询用表 WHERE  当季家禽出栏数 > 0 AND (当季家禽出栏数 / NULLIF(期末存栏只数01, 0)) NOT BETWEEN 0.2 AND 1;' WHERE " +
         " 审核公式说明 = '季度出栏/存栏[0.2,1.0]，超出范围请核实'";
            modifyOperator.ExecuteBySql(modifyformula);

        }
        catch { }
    }

    public FontWeight fontWeight { get=> FontWeight.Bold; }//本来打算给第一个按钮用的,没用上
    private async void OnMyMessageReceived(object recipient, MyMessage messageSendor)
    {
        if (messageSendor != null)
        {
            string message = messageSendor.Message;
            //  MyMessagebox(message + ",各个页面重新加载");
            PG3 = null;
            PG4 = null;
            PG5 = null;
            PG6 = null;
            PG7 = null;
               


          await  MyMessagebox.ShowAsyncInMainWindow(message + ",各个页面重新加载",Icon.Info);
        }
    }

    private void InitializeColor()
    {
        Color1 = ActiveButtonBackgroundColor;
        Color2 = InactiveButtonBackgroundColor;
        Color3 = InactiveButtonBackgroundColor;
        Color4 = InactiveButtonBackgroundColor;
        Color5 = InactiveButtonBackgroundColor;
        Color6 = InactiveButtonBackgroundColor;
        Color7 = InactiveButtonBackgroundColor;
        Color8 = InactiveButtonBackgroundColor; 
        Color9 = InactiveButtonBackgroundColor;
    }

    #endregion

    //public void ChangeBtnColor( IBrush[] BeChangedToLigthGray, IBrush BeChangedToBlue)
    //{ 
    //    if(BeChangedToLigthGray!=null)
    //    {
    //        for (int i = 0; i < BeChangedToLigthGray.Length; i++)
    //        {
    //            BeChangedToLigthGray[i] = Brushes.LightGray;
    //        }
    //    }
    //    BeChangedToBlue = Brushes.Blue;
    //}

    //private async void MyMessagebox(string message)
    //{
    //    string MyMessage = message as string;
    //    var MessageBox = MessageBoxManager.GetMessageBoxStandard("提示", MyMessage,ButtonEnum.Ok);
    //    if (MessageBox != null)
    //    {
    //        //Window window = CurrentPage as Window;
    //        //var Result = await MessageBox.ShowAsPopupAsync(owner:window);
    //        var Result = await MessageBox.ShowAsync();
    //    }
    //}
}